﻿//    Royal Bot by aluwama.
//    Copyright (C) 2008 Aluwama.
//
//    Royal Bot is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Royal Bot is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tibia;
using Tibia.Objects;
using Tibia.Util;
using System.Drawing.Drawing2D;

namespace Royal_Bot
{
    public partial class Form1 : Form
    {
        private Client client;
        private Player player;
        private Inventory inventory;
        private Tibia.Objects.Console console;
        private List<Tibia.Objects.Container> containers;
        private List<Item> items;
        private List<Item> loot;
        private ItemLocation tobp;
        private Item UH;
        private Item IH;
        private Item UHP;
        private Item GHP;
        private Item SHP;
        private Item HP;
        private Item GMP;
        private Item SMP;
        private Item MP;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            client = ClientChooser.ShowBox();
            if (client == null || !client.LoggedIn)
            {
                MessageBox.Show("You must have at least one client open and logged in.");
                Application.Exit();
            }
            else
            {
                    Player player = client.GetPlayer();
                    Map map = new Map(client);
                    inventory = new Inventory(client);
                    tobp = new ItemLocation(0, 0);
                    loot = new List<Item>();
                    client.Title = "Tibia - Royal Bot [" + (player.Name).ToString() + "]";
                    client.Statusbar = "Royal Bot: Injected Successfully.";
                    console = new Tibia.Objects.Console(client);
                    
                    UH = new Item();
                    IH = new Item();
                    UHP = new Item();
                    GHP = new Item();
                    SHP = new Item();
                    HP = new Item();
                    GMP = new Item();
                    SMP = new Item();
                    MP = new Item();
                    UH.Id = 3160;
                    IH.Id = 3152;
                    UHP.Id = 7643;
                    GHP.Id = 239;
                    SHP.Id = 236;
                    HP.Id = 266;
                    GMP.Id = 239;
                    SMP.Id = 237;
                    MP.Id = 268;
                    toolStripStatusLabel2.Text = "Name: " + (player.Name);
                   
                }
            }

        private void button1_Click(object sender, EventArgs e)
        {
            Loot();
        }
        private void Loot()
        {
            containers = inventory.GetContainers();
            for (int y = 0; y < containers.Count; y++)
            {
                if (y != tobp.container)
                {
                    items = containers[y].GetItems();
                    for (int i = 0; i < items.Count; i++)
                    {
                        if (items[i].IsInList(loot))
                        {
                            items[i].Move(tobp);
                            break;
                        }
                    }
                }
            }
        }

        private void GetBps_Click(object sender, EventArgs e)
        {
            LB_Bps.Items.Clear();
            List<Tibia.Objects.Container> BPs = inventory.GetContainers();
            for (int i = 0; i < BPs.Count; i++)
            {
                LB_Bps.Items.Add(i + ", " + BPs[i].GetItems()[0].Id.ToString());
            }
        }

        private void LB_Bps_SelectedIndexChanged(object sender, EventArgs e)
        {
            tobp.container = (byte)LB_Bps.SelectedIndex;
        }

        private void AddItem_Click(object sender, EventArgs e)
        {
            LB_Items.Items.Add(TB_Item.Text);
            loot.Add(new Item(System.Convert.ToUInt32(TB_Item.Text)));
        }

        private void RemoveItem_Click(object sender, EventArgs e)
        {
            //Item temp = );
            loot.RemoveAll(IsId);
            LB_Items.Items.RemoveAt(LB_Items.SelectedIndex);
        }
        private bool IsId(Item item)
        {
            if (item.Id == Convert.ToUInt32(LB_Items.SelectedItem)) return true;
            else return false;
        }

        private void LootTimer_Tick(object sender, EventArgs e)
        {
            Loot();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (CB_Looter.Checked == true)
            {
                LootTimer.Start();
            }
            else
            {
                LootTimer.Stop();
            }
        }

        private void CB_Xray_CheckedChanged(object sender, EventArgs e)
        {
            Map map = new Map(client);
            if (CB_Xray.Checked == true)
            {
                map.ShowNames(true);
            }
            else
            {
                map.ShowNames(false);
            }
        }

        private void CB_Antilog_CheckedChanged(object sender, EventArgs e)
        {
            if (CB_Antilog.Checked == true)
            {
                AntiLogoutTimer.Start();
            }
            else
            {
                AntiLogoutTimer.Stop();      
            }
        }

        private void CB_Light_CheckedChanged(object sender, EventArgs e)
        {
            if (CB_Light.Checked == true)
            {
                Player player = client.GetPlayer();
                player.Light = Tibia.Constants.LightSize.Full;
                player.LightColor = Tibia.Constants.LightColor.White;
            }
            else
            {
                Player player = client.GetPlayer();
                player.Light = Tibia.Constants.LightSize.None;
                player.LightColor = Tibia.Constants.LightColor.White;
            }
        }

        private void AntiLogoutTimer_Tick(object sender, EventArgs e)
        {
            Player player = client.GetPlayer();
            player.Turn(Tibia.Constants.TurnDirection.Up);
            System.Threading.Thread.Sleep(600);
            player.Turn(Tibia.Constants.TurnDirection.Down);
            System.Threading.Thread.Sleep(600);
            player.Turn(Tibia.Constants.TurnDirection.Right);
            System.Threading.Thread.Sleep(600);
            player.Turn(Tibia.Constants.TurnDirection.Left);
        }

        private void CB_ExpChecker_CheckedChanged(object sender, EventArgs e)
        {
            if (CB_ExpChecker.Checked == true)
            {
                KeyboardHook.Enable();
                KeyboardHook.KeyDown += new KeyboardHook.KeyboardHookHandler(delegate(Keys key)
                {
                    if (client.IsActive = true && key == Keys.End)
                    {                        
                        Player player = client.GetPlayer();
                        int levelNeeded = player.Level + 1;
                        double expNeeded = ((50.0 / 3.0) * Math.Pow(levelNeeded, 3)) -
                               (100.0 * Math.Pow(levelNeeded, 2)) +
                               ((850.0 / 3.0) * levelNeeded) - 200;
                        long expToGo = (long)expNeeded - player.Exp;

                        client.Statusbar = "You need " + expToGo + " experience for level " + levelNeeded + ".";

                        return false;
                    }
                    return true;

                });
            }
            else
            {
                KeyboardHook.Disable();
            }
        }

        private void CB_Title_CheckedChanged(object sender, EventArgs e)
        {
            if (CB_Title.Checked == true)
            {
                Player player = client.GetPlayer();
                client.Title = "Tibia - Royal Bot [" + (player.Name).ToString() + "]";
            }
            else
            {
                client.Title = "Tibia";
            }
        }

        private void StatusTimer_Tick(object sender, EventArgs e)
        {

            Player player = client.GetPlayer();
            locationLabel.Text = player.Location.ToString();
            M_NotifyIcon.Text = "HP: " + (player.HP.ToString()) + "/" + (player.HP_Max) + " | MP: " + (player.Mana.ToString()) + "/" + (player.Mana_Max);
        }

        private void OpenIcon_Click(object sender, EventArgs e)
        {
            OpenIconDialog.ShowDialog();
        }

        private void OpenIconDialog_FileOk(object sender, CancelEventArgs e)
        {
            M_NotifyIcon.Visible = false;
            M_NotifyIcon.Icon = (System.Drawing.Icon.ExtractAssociatedIcon(OpenIconDialog.FileName));
            comboBox1.Items.Add(OpenIconDialog.FileName).ToString();
            M_NotifyIcon.Visible = true;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            M_NotifyIcon.Visible = false;
            M_NotifyIcon.Icon = (System.Drawing.Icon.ExtractAssociatedIcon(comboBox1.SelectedItem.ToString()));
            M_NotifyIcon.Visible = true;
        }

        private void CB_AntiPara_CheckedChanged(object sender, EventArgs e)
        {
            //Antiparalyze.
            if (CB_AntiPara.Checked == true)
            {
                AntiParaTimer.Start();
            }
            else
            {
                AntiParaTimer.Stop();
            }
        }

        private void AntiParaTimer_Tick(object sender, EventArgs e)
        {
           //Antiparalyze Timer.
            if (player.HasFlag(Tibia.Constants.Flag.Paralyzed))
            {
                console.Say("Exura");                
            }
        }

        private void AddLoc_Click(object sender, EventArgs e)
        {
            Player player = client.GetPlayer();
            LB_Points.Items.Add(player.Location.ToString());
        }

        private void RemoveLoc_Click(object sender, EventArgs e)
        {
            CavebotTimer.Stop();
            LB_Points.Items.Remove(LB_Points.SelectedItem);
        }

        private void CB_Walker_CheckedChanged(object sender, EventArgs e)
        {
            //Enable/Disable Cavebot.
            if (CB_Walker.Checked == true)
            {
                CavebotTimer.Start();
            }
            else
            {
                CavebotTimer.Stop();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Set Cavebot timer interval(ms).
            if (CavebotTimer.Enabled == true)
            {
                CavebotTimer.Stop();
                CavebotTimer.Interval = (Convert.ToInt32(TB_CaveTimer));
            }
            else
            {
                CavebotTimer.Interval = (Convert.ToInt32(TB_CaveTimer));
            }            
        }

        private void CavebotTimer_Tick(object sender, EventArgs e)
        {

            //Location parsing.
            Player player = client.GetPlayer();
            string xstr = (LB_Points.SelectedItem.ToString());
            string ystr = (LB_Points.SelectedItem.ToString());
            string zstr = (LB_Points.SelectedItem.ToString());
            int x = Convert.ToInt32(xstr.Substring(1, 5));
            int y = Convert.ToInt32(ystr.Substring(8, 5));
            int z = Convert.ToInt32(zstr.Substring(15, 1));
            string newLocation = (x + "," + y + "," + z).ToString();
            player.GoTo = Tibia.Objects.Location.Parse(newLocation);
            //If player is not walking, go to next item.
            if (player.IsWalking == false)
            {

            }
        }

        private void CB_ManaTrain_CheckedChanged(object sender, EventArgs e)
        {
            if (CB_ManaTrain.Checked == true)
            {
                SpellTimer.Start();
            }
            else
            {
                SpellTimer.Stop();
            }
        }

        private void SpellTimer_Tick(object sender, EventArgs e)
        {
            Player player = client.GetPlayer();
            if (player.Mana >= (Convert.ToInt32(SpellUD.Value)))
            {
                console.Say(TB_ManaTrainS.Text.ToString());                
            }
        }

        private void CB_HPHealer_CheckedChanged(object sender, EventArgs e)
        {
            if (CB_HPHealer.Checked == true)
            {
                H_Health.Start();
            }
            else
            {
                H_Health.Stop();
            }
        }

        private void H_Health_Tick(object sender, EventArgs e)
        {
            //Simple healer.
            Player player = client.GetPlayer();
            if (player.HP < Convert.ToInt32(HealthUD.Value))
            {
                Inventory inventory = new Inventory(client);
                switch (Convert.ToString(H_HPList.SelectedItem))
                {
                    case "IH Rune":
                        inventory.UseItemOnSelf(IH.Id);
                        break;
                    case "UH Rune":
                        inventory.UseItemOnSelf(UH.Id);
                        break;
                    case "Health Potion":
                        inventory.UseItemOnSelf(HP.Id);
                        break;
                    case "Strong Health Potion":
                        inventory.UseItemOnSelf(SHP.Id);
                        break;
                    case "Great Health Potion":
                        inventory.UseItemOnSelf(GHP.Id);
                        break;
                    case "Ultimate Health Potion":
                        inventory.UseItemOnSelf(UHP.Id);
                        break;
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (H_Health.Enabled == true)
            {
                H_Health.Stop();
                H_Health.Interval = (Convert.ToInt32(textBox1.Text));
            }
            else
            {
                H_Health.Interval = (Convert.ToInt32(textBox1.Text));
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (H_Mana.Enabled == true)
            {
                H_Mana.Stop();
                H_Mana.Interval = (Convert.ToInt32(textBox1.Text));
            }
            else
            {
                H_Mana.Interval = (Convert.ToInt32(textBox1.Text));
            }
        }

        private void CB_MPHealer_CheckedChanged(object sender, EventArgs e)
        {
            if (player.Mana < Convert.ToInt32(ManaUD.Value))
            {
                Inventory inventory = new Inventory(client);
                switch (Convert.ToString(H_MPList.SelectedItem))
                {
                    case "Mana Potion":
                        inventory.UseItemOnSelf(MP.Id);
                        break;                    
                    case "Strong Mana Potion":
                        inventory.UseItemOnSelf(SMP.Id);
                        break;		    
	  	            case "Great Mana Potion":
                        inventory.UseItemOnSelf(GMP.Id);
                        break;
                    default:
                        break;
                }
	        }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            mv1.Zoom(2.00);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            mv1.Zoom(0.50);
        }

        private void CB_Map_CheckedChanged(object sender, EventArgs e)
        {
            if (CB_Map.Checked == true)
            {
                MapTimer.Start();
                mv1.Markers.Add(new MapViewer.MapMarker(player));
            }
            else
            {
                MapTimer.Stop();
            }
        }

        private void MapTimer_Tick(object sender, EventArgs e)
        {
            Player player = client.GetPlayer();
            //MapViewer code starts here
            mv1.LoadMap();
            //Wait 350ms.
            System.Threading.Thread.Sleep(350);
            mv1.SetLevel((player.Location.Z));
            mv1.SetMapCenter(player.Location);
            //MapViewer code end here
        }
    }
}
